#!/usr/bin/perl
use warnings;
use strict;
use Mysql;

# adds a post to the database

print "opening $ARGV[0]...\n";
open(FILE, "<$ARGV[0]");
my $db=Mysql->connect("server.nerdcastle.net","nerdbase","nazo","gnuisnotunix");

my $first = 1;

my $title;
my $data;

while(<FILE>)
{
    my $line = $_;
    chomp $line;
    if( $line =~ m/^Title: /)
    {
        if($first)
        {
            $first = 0;
        }
        else
        {
            $title = $db->quote($title);
            $data = $db->quote($data);
            print "adding article titled $title\n";
            $db->query("DELETE FROM posts WHERE title = $title ;");
            $db->query("INSERT INTO posts ( title, post, date ) VALUES ".
                "( $title, $data, NOW() );");
        }
        $title = substr($line, 7);
        $data = "";
    }
    elsif($line ne "")
    {
        $data .= "<p>$line</p>";
    }
}

# add the last article
$title = $db->quote($title);
$data = $db->quote($data);
print "adding article titled $title\n";
$db->query("INSERT INTO posts ( title, post, date ) VALUES ".
    "( $title, $data, NOW() );");
